//
//  BMAIntruFactory.m
//  Autismus
//
//  Created by Salhi yacine on 03/12/11.
//  Copyright (c) 2011 EPITECH. All rights reserved.
//

#import "BMAIntruFactory.h"
#import "IntruExo.h"
#import <stdlib.h>

@implementation BMAIntruFactory

#define MAX_EMOTION    6
#define MAX_IMAGE      1

-(IntruExo *)generateNewExerciceWithNbCards:(NSInteger)c
{
    BMAAppManager *appManager = [BMAAppManager sharedInstance];
    NSString *token = nil;

    // Random Emotion
    BMAEmotion rEmotion = arc4random() % MAX_EMOTION; // 0 et x-1

    NSMutableArray *photosArray = [NSMutableArray arrayWithCapacity:3];

    // Random Image Right
    for (int i = 0 ; i < (c - 1); ++i)
    {
        NSString *pathImg = [appManager randomPhotoPathFor:rEmotion];
        NSDictionary *dicImg = [NSDictionary dictionaryWithObjectsAndKeys:pathImg, @"path", [NSNumber numberWithBool:FALSE], @"answer", nil];
        [photosArray addObject:dicImg];
    }
    
    // Random Image Wrong
    for (bool end = false; end != true ;)
    {
        BMAEmotion rOther = arc4random() % MAX_EMOTION;
        if (rEmotion != rOther)
        {
            end = true;
            token = [appManager keyNameForEmotion:rOther];
            NSString *pathImg = [appManager randomPhotoPathFor:rOther];
            NSDictionary *dicImg = [NSDictionary dictionaryWithObjectsAndKeys:pathImg, @"path", [NSNumber numberWithBool:TRUE], @"answer", nil];
            [photosArray addObject:dicImg];
        }
    }
    
    // Shuffle
    NSUInteger count = [photosArray count];
    for (NSUInteger i = 0; i < count; ++i) {
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [photosArray exchangeObjectAtIndex:i withObjectAtIndex:n];
    }

    IntruExo *newExo = [[IntruExo alloc] initWithCardsArray:photosArray illustation:token];
    newExo.creationdate = [NSDate date];
    return newExo;
}

@end
